<template>
  <div class="detail-container">
    <Layout>
      <div class="main-container" ref="mainContainer" v-loading="isLoading">
        <BlogDetail :blog="data" v-if="data" />
        <BlogComment v-if="!isLoading" />
      </div>

      <template #right>
        <div class="right-container" v-loading="isLoading">
          <BLogTOC :toc="data.toc" v-if="data" />
        </div>
      </template>
    </Layout>
  </div>
</template>

<script>
import BLogTOC from "./components/BLogTOC.vue";
import fetchData from "@/mixins/fetchData";
import { getBlog } from "@/api/blog";
import BlogDetail from "./components/BlogDetail.vue";
import Layout from "@/components/Layout";
import BlogComment from "./components/BlogComment.vue";
import mainScroll from '@/mixins/mainScroll';
import {titleController} from '@/utils';
export default {
  mixins: [fetchData(null),mainScroll('mainContainer')],
  components: {
    Layout,
    BLogTOC,
    BlogDetail,
    BlogComment,
  },
  methods: {
    async fetchData() {
      const resp = await getBlog(this.$route.params.id);
      titleController.setRouteTitle(resp.title);
      return resp;
    }
  },
  updated() {
    const hash = location.hash;
    location.hash = "";
    // 由于浏览器加载完dom就改变hash看不出变化，使用异步解决
    setTimeout(() => {
      location.hash = hash;
    }, 0);
  },
};
</script>

<style lang="less" scoped>
.detail-container {
  width: 100%;
  height: 100%;
  overflow-y: scroll;
  .main-container {
    overflow-y: scroll;
    height: 100%;
    box-sizing: border-box;
    padding: 20px;
    position: relative;
    width: 100%;
    overflow-x: hidden;
    scroll-behavior: smooth;
  }
  .right-container {
    width: 300px;
    height: 100%;
    overflow-y: scroll;
    box-sizing: border-box;
    position: relative;
    padding: 20px;
  }
}
</style>